package cn.xshi.rds.web;

import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.rds.form.OMPRedisConfigForm;
import cn.xshi.rds.model.OMPRedisConfig;
import cn.xshi.rds.service.OMPRedisConfigService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desc Redis配置
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/omp/redisConfig")
@Api(value = "Redis配置",tags = "Redis配置",description = "Redis配置")
public class OMPRedisConfigController extends BaseAction {

    @Resource
    OMPRedisConfigService redisConfigService;

    /**
     * 查询Redis配置列表并分页
     * @param ompRedisConfigForm
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value="查询Redis配置列表并分页", notes="查询Redis配置列表并分页")
    @GetMapping(value="/list")
    @Auth(value = "/omp/redisConfig/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<OMPRedisConfig>> getRedisConfigListByCondition(OMPRedisConfigForm ompRedisConfigForm, Integer pageNo, Integer pageSize){
        commonHPagerByPageNo(pageNo,pageSize);
        List<OMPRedisConfig> redisConfigList = redisConfigService.getRedisConfigListByCondition(ompRedisConfigForm);
        for(OMPRedisConfig redisConfig:redisConfigList){
            redisConfigService.check(redisConfig);
        }
        PageInfo<OMPRedisConfig> page = new PageInfo<OMPRedisConfig>(redisConfigList);
        return outPageBootByPageNo(page,pageNo,pageSize);
    }

    /**
     * 查询单个Redis配置
     * @param id
     */
    @ApiOperation(value="查询单个Redis配置", notes="查询单个Redis配置")
    @GetMapping(value="/get/{id}")
    @Auth(value = "/omp/redisConfig/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<OMPRedisConfig> getRedisConfigById(@PathVariable("id")String id){
        OMPRedisConfig redisConfig = redisConfigService.getRedisConfigById(id);
        if(!StringUtil.isEmpty(redisConfig.getCreateId())){
            OauthAccountEntity createBy = getAccount(redisConfig.getCreateId());
            if(null != createBy){
                redisConfig.setCreateBy(createBy.getName());
            }
        }
        if(!StringUtil.isEmpty(redisConfig.getUpdateId())){
            OauthAccountEntity modifiedBy = getAccount(redisConfig.getUpdateId());
            if(null != modifiedBy){
                redisConfig.setModifiedBy(modifiedBy.getName());
            }
        }
        return outDataStr(redisConfig);
    }

    /**
     * 创建单个Redis配置
     * @param redisConfig
     */
    @PostMapping(value="/add")
    @ApiOperation(value="创建单个Redis配置", notes="创建单个Redis配置")
    public BaseResult addRedisConfig(@RequestBody OMPRedisConfig redisConfig){
        int i = 0;
        if(null != redisConfig){
            redisConfig.setCreateId(getCurrentAccountId());
            redisConfig.setCreateTime(getDate());
            redisConfig.setId(toUUID());
            i=redisConfigService.addRedisConfig(redisConfig);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }
    /**
     * 编辑单个Redis配置
     * @param redisConfig
     */
    @PutMapping(value="/update")
    @ApiOperation(value="编辑单个Redis配置", notes="编辑单个Redis配置")
    public BaseResult updateRedisConfig(@RequestBody OMPRedisConfig redisConfig){
        int i = 0;
        if(null != redisConfig){
            redisConfig.setUpdateId(getCurrentAccountId());
            redisConfig.setUpdateTime(getDate());
            i=redisConfigService.updateRedisConfig(redisConfig);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 删除Redis配置
     * @param id
     */
    @DeleteMapping(value="/delete")
    @ApiOperation(value="删除Redis配置", notes="删除Redis配置")
    public BaseResult delRedisConfig(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=redisConfigService.delRedisConfig(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * check运行状态
     * @param id
     */
    @ApiOperation(value="check运行状态", notes="check运行状态")
    @GetMapping(value="/check")
    @Auth(value = "/omp/redisConfig/check",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<OMPRedisConfig>> check(String id){
        if(!StringUtil.isEmpty(id)){
            OMPRedisConfigForm ompRedisConfigForm = new OMPRedisConfigForm();
            ompRedisConfigForm.setId(id);
            List<OMPRedisConfig> ompRedisConfigs =redisConfigService.getRedisConfigListByCondition(ompRedisConfigForm);
            if(!CollectionUtil.isEmpty(ompRedisConfigs)){
                for(OMPRedisConfig ompRedisConfig: ompRedisConfigs){
                    redisConfigService.check(ompRedisConfig);
                }
                return BaseResult.success(ompRedisConfigs);
            }
        }
        return BaseResult.success();
    }

    /**
     * 查询Redis配置列表
     */
    @ApiOperation(value="查询Redis配置列表", notes="查询Redis配置列表")
    @GetMapping(value="/lists")
    @Auth(value = "/omp/redisConfig/lists",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<OMPRedisConfig>> getRedisConfigList(){
        OMPRedisConfigForm ompRedisConfigForm = new OMPRedisConfigForm();
        List<OMPRedisConfig> redisConfigList = redisConfigService.getRedisConfigListByCondition(ompRedisConfigForm);
        return BaseResult.success(redisConfigList);
    }
}
